package com.exadel;

import java.util.HashMap;
import java.util.LinkedHashMap;

import junit.framework.TestCase;

public class IntLinkedHashMaptests extends TestCase {

	
	public void test_isEmpty(){
		IntLinkedHashMap my = new IntLinkedHashMap();
		assertEquals(true, my.isEmpty());
		for(int i=0;i<200;i++){
			my.put(i*20, i*66+100);
		}
		
		assertEquals(false, my.isEmpty());
	}
	public void testConstructor(){
		int capacity = 200;
		float fact = 0.50f;
		IntLinkedHashMap my = new IntLinkedHashMap(capacity);
		LinkedHashMap<Integer,Integer> hash = new LinkedHashMap<Integer,Integer>(2000, 1);
		for(int i=0;i<200;i++){
			my.put(i*20, i*66+100);
			hash.put(i*200, i*66+100);
		}
		
		int size = hash.size();
		assertEquals(size, my.size());
	}
	public void test_containsKey(){
		IntHashMap my = new IntHashMap();
		for(int i=0;i<200;i++){
			my.put(i*20, i*66+100);
		}
		
		for(int i=0;i<200;i++){
			assertEquals(true, my.containsKey(i*20));
		}
	}
	public void test_Put_HashEquals_and_KeyEquals(){
		IntHashMap my = new IntHashMap();
			my.put(20,104);
			my.put(20,1004);
			my.put(20,1024);
		
		
			assertEquals(1024, my.get(20));
		
	}
	public void test_Put_HashLinkedEquals_and_KeyEquals(){
		IntLinkedHashMap my = new IntLinkedHashMap();
			my.put(20,104);
			my.put(20,1004);
			my.put(20,1024);
		
		
			assertEquals(1024, my.get(20));
		
	}
	public void testConstructor_IllegalArgumentException(){
		int capacity = -200;
		float fact = 0.50f;
		String str = "";
		try{
		IntHashMap my = new IntHashMap(capacity,fact);
		}
		catch(IllegalArgumentException e){
			str = "IllegalArgumentException catched";
		}
		
		assertEquals("IllegalArgumentException catched", str);
	}
	public void testConstructor_IllegalArgumentException2(){
		int capacity = 200;
		float fact = -23.50f;
		String str = "";
		try{
		IntHashMap my = new IntHashMap(capacity,fact);
		}
		catch(IllegalArgumentException e){
			str = "IllegalArgumentException catched";
		}
		
		assertEquals("IllegalArgumentException catched", str);
	}
	public void testConstructor_overmaxElements(){
		int capacity = 67108865;
		float fact = 0.75f;
		IntHashMap my = new IntHashMap(capacity,fact);
		assertEquals(true, true);
	}
	public void testCorrectPut_Get1(){
		
		IntHashMap myMap = new IntHashMap();
        HashMap<Integer, Integer> otherMap = new HashMap<Integer, Integer>();
        myMap.put(0, 1004);
        myMap.put(5, 1004);
		myMap.put(20, 1024);
		
		otherMap.put(0, 1004);
		otherMap.put(5, 1004);
    	otherMap.put(20, 1024);
    	
        
        	int other = otherMap.get(5);
    	assertEquals(other, myMap.get(5));
    	 other = otherMap.get(20);
    	assertEquals(other, myMap.get(20));
    	other = otherMap.get(0);
    	assertEquals(other, myMap.get(0));
    	assertEquals(0, myMap.get(9));
    	
        }
	
	public void testCorrectPut_Get(){
		int count = 20000;
		
		IntLinkedHashMap myMap = new IntLinkedHashMap();
		LinkedHashMap<Integer, Integer> otherMap = new LinkedHashMap<Integer, Integer>();
		myMap.put(0, 1004);
		myMap.put(0, 1024);
    	assertEquals(1024, myMap.get(0));
    	
		myMap.put(5, 1004);
		myMap.put(20, 1024);
		otherMap.put(5, 1004);
    	otherMap.put(20, 1024);
    	
        
        	int other = otherMap.get(5);
    	assertEquals(other, myMap.get(5));
    	 other = otherMap.get(20);
    	assertEquals(other, myMap.get(20));
	}
	
	public void testCorrectRemove(){
		int count = 20000;
		
		IntLinkedHashMap myMap = new IntLinkedHashMap();
		LinkedHashMap<Integer, Integer> otherMap = new LinkedHashMap<Integer, Integer>();
        for(int i=1;i<count;i++){
        	myMap.put(i, i+100);
        	otherMap.put(i, i+100);
        }
        for(int i=1;i<3000;i++){
        	int other = otherMap.remove(i);
    	assertEquals(other, myMap.remove(i));
        }
	}
	public void test_ContainsValue(){
		int count = 2000;
		
		IntLinkedHashMap myMap = new IntLinkedHashMap();
        for(int i=1;i<count;i++)
        	myMap.put(i, i+100);
        
    	assertTrue(myMap.containsValue(156));
        
	}
	public void test_Not_ContainsValue(){
		int count = 2000;
		
		IntLinkedHashMap myMap = new IntLinkedHashMap();
        for(int i=1;i<count;i++)
        	myMap.put(i, i+100);
        
    	assertFalse(myMap.containsValue(56));
        
	}
	
	public void test__Not_ContainsValue_in_newLinkedHashmap(){
		int count = 10;
		
		IntLinkedHashMap myMap = new IntLinkedHashMap();
        for(int i=1;i<count;i++)
        	myMap.put(i, 32636546);
    	assertFalse(myMap.containsValue(43556)); 
	}
	
	public void testContainsValue_in_newLinkedHashmap(){
		int count = 10000;
		
		IntLinkedHashMap myMap = new IntLinkedHashMap();
        for(int i=1;i<count;i++)
        	myMap.put(i, 355+i);
    	assertTrue(myMap.containsValue(5560)); 
	}
	
	public void testToString(){
		LinkedHashMap<Integer, Integer> standart = new LinkedHashMap<Integer, Integer>();
		IntLinkedHashMap intLinked = new IntLinkedHashMap();
		standart.put(34,3);
		standart.put(7,33);
		standart.put(54,7);
		standart.put(7,644);
		standart.put(86,436);
		
		intLinked.put(34,3);
		intLinked.put(7,33);
		intLinked.put(54,7);
		intLinked.put(7,644);
		intLinked.put(86,436);
		for(int i=0;i<standart.size();i++)
			assertEquals(standart.toString().charAt(i), intLinked.toString().charAt(i));
		
		
	}
}
